package com.hb.trade.server.project.section.service.raw.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.cron.CronUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.enums.DeployStatusEnum;
import com.hb.core.pojo.parent.MPCommonEntity;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.file.entity.BidFileInfo;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.core.pojo.project.section.dto.BidSectionOfferDTO;
import com.hb.core.pojo.project.section.dto.BidSectionTenderDTO;
import com.hb.core.pojo.project.section.mapper.BidSectionTenderMapper;
import com.hb.core.pojo.project.section.entity.BidSectionTender;
import com.hb.trade.server.project.section.service.raw.BidSectionTenderService;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 投标人投标信息 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-11-09
 */
@Slf4j
@Service
public class BidSectionTenderServiceImpl extends ServiceImpl<BidSectionTenderMapper, BidSectionTender> implements BidSectionTenderService {

    @Override
    public BidSectionTenderDTO getBySectionIdAndTenderId(Long sectionId, Long tenderId) {
        BidSectionTender tender = baseMapper.getBySectionIdAndTenderId(sectionId, tenderId);
        if (tender == null) {
            return null;
        }
        return tender.to(BidSectionTenderDTO.class);
    }

    @Override
    public BidSectionTenderDTO getBySectionIdAndOrgId(Long sectionId, Long orgId) {
        BidSectionTender tender = baseMapper.getBySectionIdAndOrgId(sectionId, orgId);
        if (tender == null) {
            return null;
        }
        return tender.to(BidSectionTenderDTO.class);
    }

    @Override
    public PageResult<BidSectionTenderDTO> listBidSectionTender(BidSectionTenderDTO bidSectionTender) {
        Page page = baseMapper.listBidSectionTender(bidSectionTender.pages(), bidSectionTender);
        return PageResult.get(page);
    }

    @Override
    public List<String> getBidderUserNameBySectionId(Long sectionId) {
        if (null == sectionId) {
            log.warn("--------------------标段Id为空---------------------");
            return new ArrayList<>();
        }
        return baseMapper.getBidderUserNameBySectionId(sectionId);
    }
    /**
     *
     * @Description: 获得当前标段解密成功的投标文件列表
     * @Author: zhongdl
     * @Date:2022/2/15 15:49
     * @param sectionId:
     * @return: {@link List< BidSectionTenderDTO> }
     **/
    @Override
    public List<BidSectionTenderDTO> listDecryptionBidSectionTender(Long sectionId,Integer status,Integer decryptFlag) {
        return baseMapper.listDecryptionBidSectionTender(sectionId,status,decryptFlag);
    }


    /**
     * Description: 根据企业id和标段id查询投标文件列表
     * @author: fengbin
     * @date: 2022/2/28 16:25
     * @param orgId
     * @param sectionId
     @return: java.util.List<com.hb.core.pojo.project.section.entity.BidSection>
     */
    @Override
    public List<BidSectionTender> findSectionTenderInfoByOrgId(Long orgId, Long sectionId) {
        QueryWrapper<BidSectionTender> queryWrapper = BidSectionTender.wrapper();
        queryWrapper.eq("org_id", orgId);
        queryWrapper.eq("section_id", sectionId);
        queryWrapper.eq("status",1);
        return list(queryWrapper);
    }

    @Override
    public Long saveTenderFileInfo(BidSectionTenderDTO bidSectionTenderDTO) {
        boolean saveResult;
        Long primaryKey = bidSectionTenderDTO.getId();

        BidSectionTender tender = new BidSectionTender();
        BeanUtil.copyProperties(bidSectionTenderDTO, tender);
        if (primaryKey != null) {
            tender.buildBaseData(2);
            saveResult = updateById(tender);
        } else {
            tender.buildBaseData(1);
            saveResult = save(tender);
        }

        if (saveResult) {
            return tender.getId();
        }

        return null;
    }
}
